home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- * *
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc. *
- * All Rights Reserved. *
- * *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.; *
- * the contents of this file may not be disclosed to third parties, copied or *
- * duplicated in any form, in whole or in part, without the prior written *
- * permission of Silicon Graphics, Inc. *
- * *
- * RESTRICTED RIGHTS LEGEND: *
- * Use, duplication or disclosure by the Government is subject to restrictions *
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data *
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or *
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished - *
- * rights reserved under the Copyright Laws of the United States. *
- * *
- *******************************************************************************
- * *
- * PSORT written by *
- * Jean-Pierre Panziera *
- * jpp@corp.sgi.com *
- * *
- **************************************************************************** */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
- #include "psort.h"
-
- #define MIN(a,b) ( ((a) < (b)) ? (a) : (b))
- #define MAX(a,b) ( ((a) > (b)) ? (a) : (b))
-
- #define MAX_SIZE 111111
- #define N_MIN 100000
-
- void (*ref_sort)();
- void (*this_sort)();
-
- extern void merge_sort();
-
- double second();
-
-
- int this_compar( a, b)
- int *a, *b;
- {
- if( (*a) < (*b))
- return (-1);
- if( (*a) > (*b))
- return (1);
- return (0);
- }
-
- int min_size, max_size;
- int n_total;
- double inc_size;
- int is_speedup;
-
- main( argc, argv)
- int argc;
- char *argv[];
- {
- int log_size;
- int i,j,size, iseed, itmp, this_max;
- int *int_array, *int_copy;
-
- double t, x;
-
- GetArguments();
- srandom(getpid());
- int_array = (int *) malloc( (n_total+1) * sizeof(int ));
- int_copy = (int *) malloc( (n_total+1) * sizeof(int ));
-
- for ( size = min_size ; size <= max_size ;
- size = ( ( (i=(int)((double)size * inc_size)) > size) ? i : (size+1) ) ) {
- /*
- ************************************************
- setting up the arrays
- ************************************************
- */
- this_max = MIN(N_MIN,n_total);
- t = second();
-
- for( i = 0 ; i < this_max ; i++) {
- int_array [i] = (random() % (2* size));
- }
- int_array[this_max] = -1;
-
- #pragma parallel
- #pragma shared(size, this_max, int_array, int_copy)
- #pragma local(i)
- {
- #pragma pfor iterate(i=0;(this_max+1);1)
- for (i = 0 ; i < (this_max+1) ; i++) {
- int_copy[i] = int_array[i];
- }
- }
- t = second() - t;
-
- printf("%10d ", size);
- /*
- ************************************************
- p_sorting the array #2
- ************************************************
- */
- t = second();
-
- for( i = 0; i <= (this_max-size) ; i += size)
- this_sort( (int_copy+i), size, sizeof(int), this_compar);
-
- t = second() - t;
-
- /*
- ************************************************
- q_sorting the array #1
- ************************************************
- */
- if( is_speedup) {
- x = second();
-
- for( i = 0; i <= (this_max-size) ; i += size)
- ref_sort( (int_array+i), size, sizeof(int), this_compar);
-
- x = second() - x;
-
- x = (x > 0. && t> 0.) ? (x / t) : 0.0;
-
- }
- else {
- for ( i = 2, log_size = 1 ; i <= size ; i *= 2, log_size ++){ }
- x = (t>0) ? (1.0e-6 * (double)(size*(this_max/size)) * (double)(log_size) / t) : 0.0;
- }
- printf("%12.3f \n",x);
- /*
- ************************************************
- checking the sorted values
- ************************************************
- */
- if( int_array[this_max] != -1) {
- printf("Outside value modified \n");
- exit (-1);
- }
- /*
- ************************************************
- */
- }
- free(int_array);
- free(int_copy);
- }
-
- #include <sys/time.h>
- /* #include <sys/resource.h> */
-
- /* **********************************************************************
-
- give the elapsed wall clock time
-
- ********************************************************************** */
- double second()
- {
- struct timeval s_val;
- struct timezone s_z;
-
- static long zero_sec = 0;
- static long zero_usec = 0;
- long n_sec, n_usec;
-
- gettimeofday(&s_val, &s_z);
-
- n_sec = s_val.tv_sec;
- n_usec = s_val.tv_usec;
- if( zero_sec == 0 ) {
- zero_sec = n_sec;
- zero_usec = n_usec;
- }
-
- n_sec = n_sec - zero_sec;
- n_usec = n_usec - zero_usec;
-
- return( (double) n_sec + ( (double) n_usec * 1.0E-6 ) );
- }
-
- GetArguments( argc, argv)
- int argc;
- char *argv[];
- {
- int i, j, k;
- int nerror = 0;
-
- is_speedup = 0;
- srand( 0x01 | (123 * getpid()));
-
- min_size = 2;
- max_size = MAX_SIZE;
- inc_size = 2.0;
-
- this_sort = qsort;
- ref_sort = NULL;
- /* ******************************************************* */
- for ( i = 1 ; (i < argc) && (nerror != 1) ; i ++ ) {
- if( argv[i][0] == '-') {
- switch ( argv[i][1]) {
- case 'p' :
- case 'P' :
- this_sort = psort;
- break;
- case 'x' :
- case 'X' :
- this_sort = psort;
- ref_sort = qsort;
- is_speedup = 1;
- break;
- default : nerror = 1;
- }
- }
- else {
- if( i+3 > argc)
- nerror = 1;
- else {
- min_size = atoi( argv[i+0]);
- max_size = atoi( argv[i+1]);
- inc_size = atof( argv[i+2]);
- i += 3;
- i --;
- }
- }
- }
- n_total = MAX( max_size, N_MIN);
- /*
- printf("n_total : %d\n",n_total);
- */
- if( nerror == 1) {
- fprintf( stderr,
- "Usage : %s [-p[arallel]] [-x(speedup)] [minsize maxsize step]\n",
- argv[0]);
- exit(-1);
- }
- }
-